﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace EvePoint.Library.EventReceivers
{
    public class EvePointViewportFeatureReceiver: SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb currentWeb = properties.Feature.Parent as SPWeb;

            

            SPFeatureProperty sourceFeatureProperty = properties.Feature.Properties["SourceBrowserFilePath"];
            SPFeatureProperty destinationFeatureProperty = properties.Feature.Properties["DestinationBrowserFilePath"];
            
            string sourceBrowserFilePath=string.Format(@"{0}\{1}",properties.Definition.RootDirectory,sourceFeatureProperty.Value);
            string destinationBrowserFilePath = destinationFeatureProperty.Value;


            if (currentWeb.Site.WebApplication.IisSettings.Keys.Count > 0)
            {


                foreach (SPUrlZone copyZone in currentWeb.Site.WebApplication.IisSettings.Keys)
                {

                    SPIisSettings iisSettings = currentWeb.Site.WebApplication.IisSettings[copyZone];

                    DirectoryInfo directoryInfo = iisSettings.Path;

                    if (directoryInfo != null)
                    {
                        DirectoryInfo[] browserDirectoryArray = directoryInfo.GetDirectories("App_Browsers");

                        if (browserDirectoryArray.Length == 1)
                        {
                            if (browserDirectoryArray[0].Exists)
                            {

                                destinationBrowserFilePath = string.Format(@"{0}\{1}", browserDirectoryArray[0].FullName, destinationBrowserFilePath);

                                File.Copy(sourceBrowserFilePath, destinationBrowserFilePath, true);
                            }
                        }
                    }
                }

            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb currentWeb = properties.Feature.Parent as SPWeb;

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
